package com.xuerhua.yxt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xuerhua.yxt.common.AdminUser;
import com.xuerhua.yxt.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.AppUser;
import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.reqVo.BaseQueryVO;
import com.xuerhua.yxt.reqVo.StudentReqVO;
import com.xuerhua.yxt.reqVo.TeacherReqVO;
import com.xuerhua.yxt.service.IClassTeachersService;
import com.xuerhua.yxt.service.IOrganizeDepartService;
import com.xuerhua.yxt.service.IOrganizeGroupService;
import com.xuerhua.yxt.service.IStudentService;
import com.xuerhua.yxt.service.ISysDictDataService;
import com.xuerhua.yxt.service.ITeacherService;
import com.xuerhua.yxt.service.ITermInfoService;
import com.xuerhua.yxt.utils.StringUtil;
import com.xuerhua.yxt.view.GradeTeacherView;
import com.xuerhua.yxt.view.TeacherInfoView;
import com.xuerhua.yxt.view.TermView;
import com.xuerhua.yxt.wechat.utils.WechatGlobalMap;

@Controller
public class CompentController extends BaseController{
	
	@Autowired
	private IOrganizeDepartService organizeDepartService;
	@Autowired
	private IOrganizeGroupService organizeGroupService;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private ITeacherService teacherService;
	@Autowired
	private IClassTeachersService classTeachersService;
	@Autowired
	private ITermInfoService termInfoService;
	@Autowired
	private ISysDictDataService sysDictDataService;
	
	@RequestMapping("organizeDepart/comboBox")
	@ResponseBody Object organizeDepartComboBox() {
		return organizeDepartService.selectComboBox();
	}
	
	@RequestMapping("organizeGroup/comboBox")
	@ResponseBody Object organizeGroupComboBox() {
		String groupCode = StringUtil.emptyToNull(request.getParameter("groupCode"));
		OrganizeGroup organizeGroup = new OrganizeGroup();
		organizeGroup.setGroupCode(groupCode);
		if(!StringUtil.isNotNullOrZero(Integer.parseInt(request.getParameter("schoolId")))) {
			organizeGroup.setSchoolId(Integer.parseInt(request.getParameter("schoolId")));
		} else {
			AdminUser adminUser = getAdminUser();
			organizeGroup.setSchoolId(adminUser.getTeacherInfo().getSchoolId());
		}
		return organizeDepartService.selectGroupComboBox(organizeGroup);
	}
	
	/**
	 * 学校组织架构
	 * @return
	 */
	@RequestMapping("teacherOrganize")
	@ResponseBody Object teacherOrganize() {
		AppUser appUser = getAppUser();
		return organizeDepartService.fillTeacherOrganize(appUser.getTeacher().getSchoolId());
	}
	
	/**
	 * 学校分组架构
	 * @return
	 */
	@RequestMapping("teacherOrganizeGroup")
	@ResponseBody Object teacherOrganizeGroup() {
		AppUser appUser = getAppUser();
		OrganizeGroup og = new OrganizeGroup();
		og.setSchoolId(appUser.getTeacher().getSchoolId());
		return organizeGroupService.fillTeacherOrganizeGroup(og);
	}
	
	@RequestMapping("teacherOrganizeGroup/{groupId}")
	@ResponseBody Object teacherOrganizeGroupId(@PathVariable String groupId) {
		OrganizeGroup og = new OrganizeGroup();
		og.setIds(groupId);
		return organizeGroupService.fillTeacherOrganizeGroupByGroupIds(og);
	}
	
	/**
	 * 学生组织架构
	 * @return
	 */
	@RequestMapping("studentOrganize")
	@ResponseBody Object studentOrganize() {
		List<StudentOrganize> list = studentService.fillStudentOrganize(getCurrentTerm().getFinishYear(), getAppUser().getTeacher().getSchoolId());
		return list;
	}
	
	/**
	 * 班级架构
	 * @return
	 */
	@RequestMapping("classOrganize")
	@ResponseBody Object classOrganize() {
		AppUser appUser = getAppUser();
		StudentReqVO studentInfo = new StudentReqVO();
		studentInfo.setSchoolId(appUser.getTeacher().getSchoolId());
		studentInfo.setGrade(appUser.getTeacher().getGrade());
		studentInfo.setClazz(appUser.getTeacher().getClazz());
		return studentService.selectListByParam(studentInfo);
}
	//查询无重复班级列表
	@RequestMapping("classList")
	@ResponseBody Object classList() {
		AppUser appUser = getAppUser();
		BaseQueryVO vo = new BaseQueryVO();
		vo.setFinishYear(getCurrentTerm().getFinishYear());
		vo.setSchoolId(appUser.getTeacher().getSchoolId());
		List<ClassTeachers> list = classTeachersService.selectClassList(vo);
		return getGradeClassList(list);
	}

	/**
	 * @author hhk
	 * 查询全部数据用这个
	 */
	@RequestMapping("appClassList")
	@ResponseBody Object appClassList() {
		AppUser appUser = getAppUser();
		BaseQueryVO vo = new BaseQueryVO();
		vo.setFinishYear(getCurrentTerm().getFinishYear());
		vo.setSchoolId(appUser.getTeacher().getSchoolId());
		List<ClassTeachers> list = classTeachersService.selectClassListAll(vo);
		return getGradeClassList(list);
	}

	/**
	 * 老师列表
	 */
	@RequestMapping("teacherList")
	@ResponseBody Object teacherList() {
		AppUser appUser = getAppUser();
		TeacherReqVO vo = new TeacherReqVO();
		vo.setFinishYear(getCurrentTerm().getFinishYear());
		vo.setSchoolId(appUser.getTeacher().getSchoolId());
		List<TeacherInfo> list = teacherService.selectListForAll(vo);
		List<GradeTeacherView> viewList = new ArrayList<>();
		Map<Integer, GradeTeacherView> map = new HashMap<>();
		for(TeacherInfo teacher : list) {
			if(!map.containsKey(teacher.getGrade())) {
				GradeTeacherView view = new GradeTeacherView();
				List<TeacherInfo> list2 = new ArrayList<>();
				list2.add(teacher);
				view.setGrade(teacher.getGrade());
				view.setTeacherList(list2);
				map.put(teacher.getGrade(), view);
				viewList.add(map.get(teacher.getGrade()));
				continue;
			}
			((GradeTeacherView)map.get(teacher.getGrade())).getTeacherList().add(teacher);
		}
		map = null;
		return viewList;
	}
	
	@RequestMapping("studentList")
	@ResponseBody Object studentList(StudentReqVO vo) {
		AdminUser adminUser = getAdminUser();
		vo.setFinishYear(getCurrentTerm().getFinishYear());
		vo.setStudentName(vo.getQ());
		vo.setSchoolId(adminUser.getTeacherInfo().getSchoolId());
		return studentService.selectListByParam(vo);
}
	
	/**
	 * 老师带班列表
	 * @return
	 */
	@RequestMapping("teacherClass")
	@ResponseBody Object teacherClass() {
		AppUser appUser = getAppUser();
		List<TeacherInfoView> list = teacherService.selectClazzListByOpenid(appUser.getOpenid());
		if(list != null && list.size() > 0) {
			return list;
		}
		return studentService.fillStudentOrganize(getCurrentTerm().getFinishYear(), getAppUser().getTeacher().getSchoolId());
	}
	
	/**
	 * 我的班级
	 * @return
	 */
	@RequestMapping("myClass")
	@ResponseBody Object myClass() {
		AppUser appUser = getAppUser();
		List<TeacherInfoView> list = teacherService.selectClazzListByOpenid(appUser.getOpenid());
		return list;
	}
	
	@RequestMapping("gradeList")
	@ResponseBody Object gradeList() {
		return getCurrentTerm().getGradeMap();
	}
	
	@RequestMapping("gradeList1")
	@ResponseBody Object gradeList1() {
		return WechatGlobalMap.get(Const.CACHE_GRADE).getValue();
	}
	
	@RequestMapping("subjectList")
	@ResponseBody Object subjectList() {
		return WechatGlobalMap.get(Const.CACHE_SUBJECT).getValue();
	}
	
	@RequestMapping("valid")
	@ResponseBody Object valid() {
		List<ComboBox> list = new ArrayList<>();
		list.add(new ComboBox("N", "有效"));
		list.add(new ComboBox("Y", "失效"));
		return list;
	}

	/**
	 * hhk：“大概是给手机端用的？”
	 */
	@RequestMapping("termList")
	@ResponseBody Object selectList(Integer schoolId) {
		return termInfoService.selectList(schoolId);
	}

	/**
	 * hhk:"上面那个方法不知道后台有没有用到，不敢动，为手机端写新方法"
	 */
	@RequestMapping("termListForPhone")
	@ResponseBody Object selectListForPhone() {
		Integer schoolId = getAppUser().getRoleType()==1 ? getAppUser().getTeacher().getSchoolId() : getAppUser().getParent().getSchoolId();
		return termInfoService.selectList(schoolId);
	}

	@RequestMapping("gradeCombo")
	@ResponseBody Object gradeCombo() {
		Map<Integer, String> map = getCurrentTerm().getGradeMap();
		List<ComboBox> list = new ArrayList<>();
		for (Map.Entry<Integer, String> entry : map.entrySet()) {
			list.add(new ComboBox(String.valueOf(entry.getKey()), entry.getValue()));
		}
		return list;
	}
	
	@RequestMapping("subjectCombo")
	@ResponseBody Object subjectCombo() {
		Map<Integer, String> map = (Map<Integer, String>) WechatGlobalMap.get(Const.CACHE_SUBJECT).getValue();
		List<ComboBox> list = new ArrayList<>();
		for (Map.Entry<Integer, String> entry : map.entrySet()) {
			list.add(new ComboBox(String.valueOf(entry.getKey()), entry.getValue()));
		}
		return list;
	}
	
	@RequestMapping("dictData/dictCode")
	@ResponseBody Object dictDataDictCode(SysDictType sysDictType) {
		return sysDictDataService.selectListByDictCode(sysDictType);
	}
	
	@RequestMapping("gradeByTermYear")
	@ResponseBody Object gradeByTermYear(Integer termYear) {
		return TermView.getGradeMap(termYear);
	}
	
	@RequestMapping("getFullGrade")
	@ResponseBody Object getFullGrade() {
		return studentService.getFullGrade();
	}
	
	@RequestMapping("getFullClazz")
	@ResponseBody Object getFullClazz() {
		String grade = request.getParameter("grade");
		return studentService.getFullClazz(Integer.valueOf(grade));
	}
}
